package lab;

import process.Actor;
import process.IWaitCondition;
import process.QueueForTransactions;
import rnd.Randomable;

public class Gruzovik extends Actor {
	
	private double finishTime;
	private QueueForTransactions queuegruzovikOnRoad;
	private QueueForTransactions queueToKran;
	private Randomable rnd;
	private int body=0;
	
	public Gruzovik(String name, mainGUI gui, Model model){
		this.setNameForProtocol(name);
		this.queueToKran = model.getqGruzovik();
		this.queuegruzovikOnRoad = model.getqGruzovikOnRoad();
		this.finishTime = gui.getTimeWork().getDouble();
		this.rnd = gui.getTimeGruzovikRandom();
	}


	@Override
	protected void rule() {
		// TODO Auto-generated method stub
		IWaitCondition bodyIsFull = new IWaitCondition() {
			public boolean testCondition() {
				return isFull();
			}
			public String toString() {
				return ("pogryzil konteyner");
			}
		};
		
		while (getDispatcher().getCurrentTime() <= finishTime) {
			
			queueToKran.addLast(this);
		
			try {
				waitForCondition(bodyIsFull);
			} catch (Exception e) {
				return;
			}
			
			queueToKran.remove(this);
			
			queuegruzovikOnRoad.add(this);
			holdForTime(rnd.next());
			
			queuegruzovikOnRoad.remove(this);
			getDispatcher().printToProtocol(getNameForProtocol()+"rozvantagyetsa");
			
			body = 0;
			
		}
	}


	
public void addPortion() {
	body++;
	getDispatcher().printToProtocol(
			getNameForProtocol()+" - zagryzil? vezi!");
}
	
	public boolean isFull() {
		return body >= 1;
	}


}
